﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Coding4Fun.Cannon.Robot;
using Coding4Fun.Cannon.Web.Common;

namespace Coding4Fun.Cannon.Web.RcCar
{
	public class PhidgetHttpHandler : CommonHttpHandler
	{
		static readonly PhidgetInterfaceKit Controller = new PhidgetInterfaceKit();

		public override void Drive(int power)
		{
			Drive(power, power);
		}

		public override void Drive(int leftMotor, int rightMotor)
		{
			if (leftMotor + rightMotor > 0)
			{
				Controller.Open(0);
				Controller.Close(1);
			}
			else if (leftMotor + rightMotor < 0)
			{
				Controller.Open(1);
				Controller.Close(0);
			}
			else if (leftMotor == 0 && rightMotor == 0)
			{
				Controller.Close(0);
				Controller.Close(1);
			}

			if (leftMotor < rightMotor)
			{
				Controller.Open(2);
				Controller.Close(3);
			}
			else if (leftMotor > rightMotor)
			{
				Controller.Open(3);
				Controller.Close(2);
			}
			else
			{
				Controller.Close(2);
				Controller.Close(3);
			}
		}
	}
}